package rogueliketest;

import java.util.Random;
import java.util.Scanner;

/**
 * 游戏主类
 */
public class rogueGame {
    private static final int MAP_SIZE = 8;
    private static final int MAX_LEVEL = 5;
    private Player player;
    private char[][] map;
    private int currentLevel;
    private Random random;

    public static void main(String[] args) {
        new rogueGame().startGame();
    }

    public void startGame() {
        random = new Random();
        player = new Player();
        currentLevel = 1;
        System.out.println("=== 地牢探险 ===");
        generateNewMap();
        gameLoop();
    }

    // 后续代码将逐步补充
    
/**
 * 实现地图生成逻辑
 */
private void generateNewMap() {
    map = new char[MAP_SIZE][MAP_SIZE];
    // 填充空地
    for(int i=0; i<MAP_SIZE; i++){
        for(int j=0; j<MAP_SIZE; j++){
            map[i][j] = random.nextDouble() < 0.3 ? '#' : '.';
        }
    }
    // 设置玩家起始位置
    map[0][0] = 'P';
    // 设置楼梯位置
    map[MAP_SIZE-1][MAP_SIZE-1] = 'S';
}

/**
 * 加游戏主循环
 */
private void gameLoop() {
    Scanner scanner = new Scanner(System.in);
    while(player.isAlive() && currentLevel <= MAX_LEVEL){
        printMap();
        System.out.print("移动方向(W上/S下/A左/D右): ");
        String input = scanner.next().toUpperCase();
        handleMovement(input.charAt(0));
        
        // 30%概率遭遇敌人
        if(random.nextDouble() < 0.3){
            startBattle();
        }
    }
    scanner.close();
}

/**
 * 实现战斗系统
 */
private void startBattle() {
    Enemy enemy = generateEnemy();
    System.out.println("遭遇了 " + enemy.getName() + "!");
    
    while(player.isAlive() && enemy.isAlive()){
        // 玩家攻击
        enemy.takeDamage(player.getAttack());
        System.out.println("你对敌人造成 " + player.getAttack() + " 点伤害");
        
        if(enemy.isAlive()){
            // 敌人反击
            player.takeDamage(enemy.getAttack());
            System.out.println("敌人对你造成 " + enemy.getAttack() + " 点伤害");
        }
    }
    
    if(player.isAlive()){
        System.out.println("战斗胜利！获得金币");
        player.addGold(random.nextInt(5)+1);
    } else {
        System.out.println("游戏结束！");
    }
}

private Enemy generateEnemy() {
    String[] names = {"哥布林", "骷髅兵", "毒蜘蛛"};
    String name = names[random.nextInt(names.length)];
    int baseHealth = currentLevel * 5;
    int baseAttack = currentLevel * 2;
    return new Enemy(name, baseHealth + random.nextInt(3), baseAttack + random.nextInt(2));
}

/**
 * 添加地图显示和移动处理
 */
private void printMap() {
    System.out.println("\n当前楼层：" + currentLevel);
    for(char[] row : map){
        for(char cell : row){
            System.out.print(cell + " ");
        }
        System.out.println();
    }
}

/**
 * 寻找玩家当前位置
 */
private int[] findPlayerPosition() {
    for(int i=0; i<MAP_SIZE; i++) {
        for(int j=0; j<MAP_SIZE; j++) {
            if(map[i][j] == 'P') {
                return new int[]{i, j};
            }
        }
    }
    throw new RuntimeException("玩家不在地图上！");
}

private void handleMovement(char direction) {
    // 寻找玩家当前位置
    int[] pos = findPlayerPosition();
    int x = pos[0], y = pos[1];
    
    // 旧方法
    // try {
    //     switch(direction) {
    //         case 'W' : x--;
    //         case 'S' : x++;
    //         case 'A' : y--;
    //         case 'D' : y++;
    //     }
        
    //     if(map[x][y] == '#') {
    //         System.out.println("撞到墙了！");
    //         return;
    //     }
        
    //     if(map[x][y] == 'S') {
    //         if(currentLevel < MAX_LEVEL) {
    //             currentLevel++;
    //             generateNewMap();
    //             return;
    //         } else {
    //             System.out.println("恭喜通关！");
    //             System.exit(0);
    //         }
    //     }
        
    //     // 更新玩家位置
    //     map[pos[0]][pos[1]] = '.';
    //     map[x][y] = 'P';
    // } catch (ArrayIndexOutOfBoundsException e) {
    //     System.out.println("不能移出地图外！");
    // }

    // 新方法
    // 计算新位置
    int newX = x, newY = y;
    switch(direction) {
        case 'W' : newX--;
        case 'S' : newX++;
        case 'A' : newY--;
        case 'D' : newY++;
    }
    
    // 边界检查
    if(newX < 0 || newX >= MAP_SIZE || newY < 0 || newY >= MAP_SIZE) {
        System.out.println("不能移出地图外！");
        return;
    }
    
    // 碰撞检测
    if(map[newX][newY] == '#') {
        System.out.println("撞到墙了！");
        return;
    }
    
    // 楼梯处理
    if(map[newX][newY] == 'S') {
        if(currentLevel < MAX_LEVEL) {
            currentLevel++;
            generateNewMap();
            return;
        } else {
            System.out.println("恭喜通关！");
            System.exit(0);
        }
    }
    
    // 更新位置
    map[x][y] = '.';
    map[newX][newY] = 'P';
}
}


/**
 * 添加玩家和敌人类
 */
class Player {
    private int health;
    private int attack;
    private int gold;
    
    public Player() {
        health = 20;
        attack = 3;
        gold = 0;
    }
    
    public boolean isAlive() {
        return health > 0;
    }

    public void takeDamage(int damage) {
        health -= damage;
    }

    public void addGold(int amount) {
        gold += amount;
    }

    // Getter方法
    public int getHealth() { return health; }
    public int getAttack() { return attack; }
    public int getGold() { return gold; }
}

class Enemy {
    private String name;
    private int health;
    private int attack;
    
    public Enemy(String name, int health, int attack) {
        this.name = name;
        this.health = health;
        this.attack = attack;
    }
    
    public boolean isAlive() {
        return health > 0;
    }

    public void takeDamage(int damage) {
        health -= damage;
    }

    // Getter方法
    public String getName() { return name; }
    public int getHealth() { return health; }
    public int getAttack() { return attack; }
}
